<?php
/**
 * Главная страница
 */
function pageMain() {
	TPL::Load('page.main');
	// марки машин
	if (Cache::ReadHTML('main_marks')) {TPL::setBlock('marks', Cache::Get('main_marks'));}
	else {
		$save_cache = TRUE;
		$_SELECT    = MySQL::Select('SELECT `mark_eng`, `mark_alias` FROM `{p}marks`', MYSQL_SQUERY);
		if (!$_SELECT) {
			TPL::makeBlock('marks', 'nomarks', array ('text' => 'Не удалось получить список марок машин'), 'w');
			$save_cache   = FALSE;
		} else {
			while (($mark = MySQL::Fetch_array($_SELECT)) !== NULL) {
				TPL::makeBlock('marks', 'marks', array (
					'text' => $mark['mark_eng'],
					'href' => Conf::$site['site_url'].'/mark/'.$mark['mark_alias']
				)); 
			}
		}
		if ($save_cache) {Cache::WriteHTML('main_marks', TPL::getBlock('marks'));}
	}
	// последние 10 добавленных машин
	$save_cache = TRUE;
	if (Cache::ReadHTML('main_last10')) {TPL::setBlock('last10', Cache::Get('main_last10'));}
	else {
		$_SELECT = MySQL::Select('SELECT `car_id` FROM `{p}cars` ORDER BY `car_dateadd` DESC LIMIT 10', MYSQL_SALL);
		if (!$_SELECT) {
			TPL::makeBlock('last10', 'nocars', array ('text' => 'Не удалось получить список машин'));
			$save_cache = FALSE;
		} else {
			$car_ids = F::IDstoSTR($_SELECT);
			$_SELECT = MySQL::Select('SELECT `car_year`, `car_name`, `car`.`car_id`, `car_price`, `car_pricecur`, `img_thumb` FROM `{p}cars` `car` INNER JOIN `{p}images` `i` ON `i`.`car_id` = `car`.`car_id` INNER JOIN `{p}prices` `p`  ON `p`.`car_id` = `car`.`car_id`WHERE `car`.`car_id` IN ('.$car_ids.') GROUP BY `car`.`car_id`', MYSQL_SQUERY);
			if (!$_SELECT) {
				TPL::makeBlock('last10', 'nocars', array ('text' => 'Не удалось получить список машин'));
				$save_cache = FALSE;
			} else {
				$name_replace = array ('*' => ' ', ';' => ' ', '..' => ' ', ',' => ' ');
				while (($car = MySQL::Fetch_array($_SELECT)) !== NULL) {
					TPL::makeBlock('last10', 'cars', array (
						'src'  => ($car['img_thumb'] != '') ? ($car ['img_thumb']) : ('/images/no_car.gif'),
						'href' => Conf::$site['site_url'].'/car/'.$car['car_id'],
						'name' => strtr($car['car_name'], $name_replace),
						'text' => (($car['car_year'] != 0)?($car['car_year'].' год<br />'):('')).$car['car_price'].' '.$car['car_pricecur']
					));
				}
			}	
		}
		if ($save_cache) {Cache::WriteHTML('main_last10', TPL::getBlock('last10'));}
	}
	return 0;
}
?>
